package UseUtil_Test;

import util.jdbc_util_C3P0;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;


//DBCP 利用别人封装好的工具类（连接池）
public class util_C3P0 {
    public static void main(String[] args) {
        PreparedStatement Statement = null;
        Connection connection = null;//原来是自己实现的，现在使用别人实现的
        try {
            connection = jdbc_util_C3P0.get_connection();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        //区别:
        //使用 ？ 占位符代替参数，插入语句
        String sql = "insert into `users`(`id`,`name`,`password`,`email`,`birthday`)values (?,?,?,?,?)";
        int num = 0;

        //手动给参数赋值
        try {
            Statement = connection.prepareStatement(sql);//需要一个预编译SQL，先写SQL，不执行
            Statement.setInt(1, 7);
            Statement.setString(2, "郭八1");
            Statement.setString(3, "1223456");
            Statement.setString(4, "31233@qq.com");
            Statement.setDate(5, new Date(new java.util.Date().getTime()));
            num = Statement.executeUpdate();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            try {
                jdbc_util_C3P0.release(connection, Statement, null);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }


        if (num > 0) {
            System.out.println("更新成功");
        }


    }
}
